package com.neusoft.neuiotms.dcam.fixed.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.model.InventoryPlan;
import com.neusoft.neuiotms.dcam.fixed.model.QInventoryPlan;
import com.neusoft.neuiotms.dcam.fixed.model.enums.InventoryPlanStatus;
import com.neusoft.neuiotms.dcam.fixed.repository.CustomizedInventoryPlanRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedInventoryPlanRepositoryImpl extends AbstractRepository
        implements CustomizedInventoryPlanRepository {

    @Override
    public List<InventoryPlan> search(final Searchable searchable) {
        final QInventoryPlan qInventoryPlan = QInventoryPlan.inventoryPlan;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<InventoryPlan> query = this.query().selectFrom(qInventoryPlan);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<InventoryPlan> search(final Pageable pageable, final Searchable searchable) {
        final QInventoryPlan qInventoryPlan = QInventoryPlan.inventoryPlan;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<InventoryPlan> query = this.query().selectFrom(qInventoryPlan);
        query.where(where);
        //.orderBy(qInventoryPlan.updateTime.desc(),
        //              qInventoryPlan.code.desc(),
        //            qInventoryPlan.status.desc());
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return InventoryPlan.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QInventoryPlan qInventoryPlan = QInventoryPlan.inventoryPlan;

        // 计划名称
        if (!StringUtils.isEmpty(searchable.getStrValue("name"))) {
            where.or(super.containsStr(qInventoryPlan.name, searchable, "name"));
        }

        // 计划编号
        if (!StringUtils.isEmpty(searchable.getStrValue("name"))) {
            where.or(super.containsStr(qInventoryPlan.code, searchable, "name"));
        }

        // 计划ID
        if (null != searchable.getLongValue("id")) {
            where.and(super.equalsLong(qInventoryPlan.id, searchable, "id"));
        }

        // 计划状态
        if (!StringUtils.isEmpty(searchable.getStrValue("status"))) {
            where.and(super.equalsEnum(qInventoryPlan.status, searchable, "status",
                    value -> InventoryPlanStatus.valueOf(value)));
        }

        // 所属
        if (!StringUtils.isEmpty(searchable.getStrValue("belongs"))) {
            where.or(super.containsStr(qInventoryPlan.belongs, searchable, "belongs"));
        }

        return where;
    }
}
